<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="甲方识别码" prop="ah">
        <el-input
          v-model="queryParams.nbajbh"
          placeholder="请输入甲方识别码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="委托方批次">
        <el-select v-model="queryParams.zfid" placeholder="请选择委托方" clearable @change="getBatch">
          <el-option
            v-for="item in zfOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
        <el-select v-model="queryParams.batchId" placeholder="请选择批次" clearable @change="handleQuery">
          <el-option
            v-for="item in pcOptions"
            :key="item.id"
            :label="item.batchName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="诉讼主体">
        <el-select v-model="queryParams.ssztid" placeholder="请选择诉讼主体" clearable @change="handleQuery">
          <el-option
            v-for="item in ssztOptions"
            :key="item.id"
            :label="item.dwmc"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="被告" prop="ajmc">
        <el-input
          v-model="queryParams.ajmc"
          placeholder="请输入被告名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="案号" prop="ah">
        <el-input
          v-model="queryParams.ah"
          placeholder="请输入案号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法院名称" prop="fymc">
        <el-input
          v-model="queryParams.fymc"
          placeholder="请输入法院名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开庭方式" prop="ktfs">
        <el-select v-model="queryParams.ktfs" placeholder="请选择开庭方式" clearable>
          <el-option
            v-for="dict in dict.type.fs_aj_ktlx"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开庭时间" prop="ktrq">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="证据准备" prop="zjzb">
        <el-input
          v-model="queryParams.zjzb"
          placeholder="请输入证据准备"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fs:opencourt:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fs:opencourt:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fs:opencourt:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fs:opencourt:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="opencourtList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="进展id" align="center" prop="processId"/>
      <el-table-column label="案号" align="center" prop="ah"/>
      <el-table-column label="案件id" align="center" prop="caseId"/>
      <el-table-column label="法院id" align="center" prop="fyid"/>
      <el-table-column label="法院名称" align="center" prop="fymc"/>
      <el-table-column label="法庭" align="center" prop="ft"/>
      <el-table-column label="开庭方式" align="center" prop="ktfs">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fs_aj_ktlx" :value="scope.row.ktfs"/>
        </template>
      </el-table-column>
      <el-table-column label="开庭日期" align="center" prop="ktrq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ktrq, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开庭时间" align="center" prop="ktsj"/>
      <el-table-column label="调解员" align="center" prop="tjy"/>
      <el-table-column label="法官信息" align="center" prop="fgxx"/>
      <el-table-column label="代理律师id" align="center" prop="dllsid"/>
      <el-table-column label="代理律师名称" align="center" prop="dlls"/>
      <el-table-column label="律师证件存放地址" align="center" prop="lszjUrl"/>
      <el-table-column label="开庭短信" align="center" prop="ktdx"/>
      <el-table-column label="开庭传票信息" align="center" prop="ktcpxx"/>
      <el-table-column label="开庭传票存放地址" align="center" prop="ktcpUrl"/>
      <el-table-column label="线上小程序信息" align="center" prop="xcxxx"/>
      <el-table-column label="调解方案" align="center" prop="tjfa"/>
      <el-table-column label="证据准备" align="center" prop="zjzb"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fs:opencourt:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fs:opencourt:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改案件进展开庭对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="案号" prop="ah">
          <el-input v-model="form.ah" placeholder="请输入案号"/>
        </el-form-item>
        <el-form-item label="案件id" prop="caseId">
          <el-input v-model="form.caseId" placeholder="请输入案件id"/>
        </el-form-item>
        <el-form-item label="法院id" prop="fyid">
          <el-input v-model="form.fyid" placeholder="请输入法院id"/>
        </el-form-item>
        <el-form-item label="法院名称" prop="fymc">
          <el-input v-model="form.fymc" placeholder="请输入法院名称"/>
        </el-form-item>
        <el-form-item label="法庭" prop="ft">
          <el-input v-model="form.ft" placeholder="请输入法庭"/>
        </el-form-item>
        <el-form-item label="开庭方式" prop="ktfs">
          <el-select v-model="form.ktfs" placeholder="请选择开庭方式">
            <el-option
              v-for="dict in dict.type.fs_aj_ktlx"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开庭日期" prop="ktrq">
          <el-date-picker clearable
                          v-model="form.ktrq"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择开庭日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开庭时间" prop="ktsj">
          <el-input v-model="form.ktsj" placeholder="请输入开庭时间"/>
        </el-form-item>
        <el-form-item label="调解员" prop="tjy">
          <el-input v-model="form.tjy" placeholder="请输入调解员"/>
        </el-form-item>
        <el-form-item label="法官信息" prop="fgxx">
          <el-input v-model="form.fgxx" placeholder="请输入法官信息"/>
        </el-form-item>
        <el-form-item label="代理律师id" prop="dllsid">
          <el-input v-model="form.dllsid" placeholder="请输入代理律师id"/>
        </el-form-item>
        <el-form-item label="代理律师名称" prop="dlls">
          <el-input v-model="form.dlls" placeholder="请输入代理律师名称"/>
        </el-form-item>
        <el-form-item label="律师证件存放地址" prop="lszjUrl">
          <el-input v-model="form.lszjUrl" placeholder="请输入律师证件存放地址"/>
        </el-form-item>
        <el-form-item label="开庭短信" prop="ktdx">
          <el-input v-model="form.ktdx" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="开庭传票信息" prop="ktcpxx">
          <el-input v-model="form.ktcpxx" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="开庭传票存放地址" prop="ktcpUrl">
          <el-input v-model="form.ktcpUrl" placeholder="请输入开庭传票存放地址"/>
        </el-form-item>
        <el-form-item label="线上小程序信息" prop="xcxxx">
          <el-input v-model="form.xcxxx" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="调解方案" prop="tjfa">
          <el-input v-model="form.tjfa" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="证据准备" prop="zjzb">
          <el-input v-model="form.zjzb" placeholder="请输入证据准备"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOpencourt, getOpencourt, delOpencourt, addOpencourt, updateOpencourt} from "@/api/fs/opencourt";
import {getRepaymentByCaseId, listBatch, listSszt, listZf} from "@/api/fs/case";

export default {
  name: "Opencourt",
  dicts: ['fs_aj_ktlx'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 案件进展开庭表格数据
      opencourtList: [],
      // 日期范围
      dateRange: [],
      zfOptions: [],
      pcOptions: [],
      ssztOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processId: null,
        ah: null,
        caseId: null,
        fyid: null,
        fymc: null,
        ft: null,
        ktfs: null,
        ktrq: null,
        ktsj: null,
        tjy: null,
        fgxx: null,
        dllsid: null,
        dlls: null,
        lszjUrl: null,
        ktdx: null,
        ktcpxx: null,
        ktcpUrl: null,
        xcxxx: null,
        tjfa: null,
        zjzb: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getZf();
    this.getSszt();
    this.getList();
  },
  methods: {
    getZf() {
      listZf().then(response => {
        this.zfOptions = response.data;
      });
    },
    getBatch() {
      this.caseParams.batchId = null;
      this.pcOptions = [];
      this.handleQuery();
      if (this.caseParams.zfid && this.caseParams.zfid > 0) {
        listBatch(this.caseParams.zfid).then(response => {
          this.pcOptions = response.data;
        });
      }
    },
    getSszt() {
      listSszt().then(response => {
        this.ssztOptions = response.data;
      });
    },
    /** 查询案件进展开庭列表 */
    getList() {
      let params = {};
      if (this.queryParams.nbajbh) {
        params['nbajbh'] = this.queryParams.nbajbh;
      }
      if (this.queryParams.ajmc) {
        params['ajmc'] = this.queryParams.ajmc;
      }
      if (this.queryParams.zfid) {
        params['zfid'] = this.queryParams.zfid;
      }
      if (this.queryParams.batchId) {
        params['batchId'] = this.queryParams.batchId;
      }
      if (this.queryParams.ssztid) {
        params['ssztid'] = this.queryParams.ssztid;
      }
      this.queryParams.params = params;
      this.loading = true;
      listOpencourt(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.opencourtList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        processId: null,
        ah: null,
        caseId: null,
        fyid: null,
        fymc: null,
        ft: null,
        ktfs: null,
        ktrq: null,
        ktsj: null,
        tjy: null,
        fgxx: null,
        dllsid: null,
        dlls: null,
        lszjUrl: null,
        ktdx: null,
        ktcpxx: null,
        ktcpUrl: null,
        xcxxx: null,
        tjfa: null,
        zjzb: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加案件进展开庭";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOpencourt(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改案件进展开庭";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOpencourt(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOpencourt(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除案件进展开庭编号为"' + ids + '"的数据项？').then(function () {
        return delOpencourt(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fs/opencourt/export', {
        ...this.queryParams
      }, `opencourt_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
